﻿using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using Portfolio.Models;

namespace Portfolio.Persistencia
{
    public class Covers
    {
        public static IEnumerable<Cover> ListarCovers()
        {
            List<Cover> covers = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = "SELECT c.Ordem, f.NomeImagem FROM Cover c INNER JOIN Fotos f ON c.IdFoto = f.Id WHERE f.IdEvento IS NULL"
                };

                conn.Open();

                try
                {
                    MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        covers = new List<Cover>();

                        while (reader.Read())
                        {
                            covers.Add(new Cover()
                            {
                                Ordem = (int)reader["Ordem"],
                                NomeImagem = reader["NomeImagem"].ToString()
                            });
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return covers;
        }

        public static string BuscarCover(int idEvento)
        {
            string cover = string.Empty;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = string.Format("SELECT f.NomeImagem FROM Cover c INNER JOIN Fotos f ON c.IdFoto = f.Id WHERE c.Ordem IS NULL AND f.IdEvento = {0}", idEvento)
                };

                conn.Open();

                try
                {
                    MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.Read())
                    {
                        cover = reader["NomeImagem"].ToString();
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return cover;
        }
    }
}